package com.limetalks.data;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@NamedQueries({
	@NamedQuery(name="findByMaster", query="SELECT s FROM Stream s where s.master = :master")
})
@Table(name = "stream")
public class Stream
{
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "stream_id")
	private Integer id;
	
	private String name;
	
	private String image;

	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="master", unique= true, nullable=true, insertable=true, updatable=true)
	private User master;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="category")
	private Category category;
	
	public enum StreamStatus {PAST,CURRENT,FUTURE};
	
	@Column(name = "status")
	@Enumerated(EnumType.STRING)
	private StreamStatus status;

	public StreamStatus getStatus() {
		return status;
	}

	public void setStatus(StreamStatus status) {
		this.status = status;
	}

	@Transient
	private Set<User> users = new HashSet<User>();
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}	

	public void setName(String name) {
		this.name = name;
	}
	
	public Set<User> getUsers() {
		return users;
	}
	
	public User getMaster() {
		return master;
	}

	public void setMaster(User master) {
		this.master = master;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	
	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Stream other = (Stream) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}	
	
	@Override
	public String toString() {
		return "stream [id = " + getId() + ", name = '"+ getName() +"', master = " + master + "]";
	}

	public void addUser(User user) {
		getUsers().add(user);
	}
	
	public void removeUser(User user) {
		getUsers().remove(user);
	}
}
